#include "circle.h"
#include "link.h"
#include "circleproperty.h"

circle::circle(designer *d)
{
    this->myDesigner = d;

    this->setTextColor(Qt::darkGreen);

    this->setOutlineColor(Qt::darkBlue);

    this->setBackgroundColor(Qt::white);

    this->setType("circle");

    setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsGeometryChanges);

    this->node = new xmlNode();
}

circle::~circle()
{
    foreach (link *link, this->links())
    {
        delete link;
    }
    QList<circle *> temp=this->myDesigner->getCircleList();//cancello il circle dalla lista dei circle
    temp.removeOne(this);
    this->myDesigner->setCircleList(temp);
}


QRectF circle::outlineRect() const
{
    const int Padding = 8; //8 pixel di margine
    QFontMetricsF metrics = qApp->font();
    QRectF rect = metrics.boundingRect(this->text());
    rect.adjust(-Padding, -Padding, +Padding, +Padding);
    rect.translate(-rect.center());
    return rect;
}



QRectF circle::boundingRect() const
{
    const int Margin = 1;
    return outlineRect().adjusted(-Margin, -Margin, +Margin, +Margin);
}


QPainterPath circle::shape() const
{
    QRectF rect = outlineRect();

    QPainterPath path;
    path.addRoundRect(rect, roundness(rect.width()),
                      roundness(rect.height()));
    return path;
}


void circle::paint(QPainter *painter,
                 const QStyleOptionGraphicsItem *option,
                 QWidget * /* widget */)
{
    QPen pen(this->outlineColor());
    if (option->state & QStyle::State_Selected) {
        pen.setStyle(Qt::DotLine);
        pen.setWidth(2);
    }
    painter->setPen(pen);
    painter->setBrush(this->backgroundColor());

    QRectF rect = outlineRect();
    painter->drawEllipse(rect);

    painter->setPen(this->textColor());
    painter->drawText(rect, Qt::AlignCenter, this->text());
}


int circle::roundness(double size) const
{
    const int Diameter = 12;
    return 100 * Diameter / int(size);
}

void circle::setNode(xmlNode *n)
{
    this->node = n;
}

xmlNode *circle::getNode()
{
    return this->node;
}

void circle::setIsSet(bool value)
{
    this->isSet = value;
}

bool circle::getIsSet()
{
    return this->isSet;
}

void circle::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if(this->getIsSet() == false)
    {
        circleproperty *dialog = new circleproperty(this, this->myDesigner);
        dialog->lineEdit->setText(this->getNode()->getObj_id());
        dialog->show();
    }
    else
    {
        circleproperty *dialog = new circleproperty(this, this->myDesigner);
        dialog->lineEdit->setText(this->getNode()->getObj_id());
        dialog->lineEdit_2->setText(this->getNode()->getNum_term());

        QString terminalList = this->getNode()->getTerminal_node_list().join(" ");
        dialog->lineEdit_3->setText(terminalList);

        dialog->show();
    }
}

void circle::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
 {
     QMenu menu;
     QAction *removeAction = menu.addAction("Remove");
     connect(removeAction, SIGNAL(triggered()), this, SLOT(deleteCircle()));
     menu.exec(event->screenPos());
 }

void circle::deleteCircle()
{
    this->~circle();
    this->myDesigner->itemIsRemoved();
    //this->myDesigner->deleteCircle(this);
}
